package com.demo.service.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;

import com.demo.employee.HibernateUtils;

public class HibernateDaoService {


	public Object fetchObjectFromDatabase( Class  entityClass,int objectId){

		Session session=HibernateUtils.getSessionFactory().openSession();
		session.beginTransaction();
		
		Object object= session.get(entityClass, objectId);
		session.getTransaction().commit();
		session.close();
		return object;

	}


	public void insertNewRow(Object objectToInsert) {

		Session session=HibernateUtils.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(objectToInsert);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}


	public List<?> loadAllRowsForClass(Class classToLoad){

		Session session=HibernateUtils.getSessionFactory().openSession();
		session.beginTransaction();
		//criteria is used to perfrom select queries.. 
		Criteria criteria=  session.createCriteria(classToLoad);
		List<?> list=  criteria.list();
		session.getTransaction().commit();
		session.close();
		return list;


	}

	public List<?> loadAllRowsFroDetachedCriteria(DetachedCriteria criteria){
		List<?> list=null;	
		Session session=	HibernateUtils.getSessionFactory().openSession();
		session.beginTransaction();
		list=  criteria.getExecutableCriteria(session).list();
		session.getTransaction().commit();
		session.close();
		return list;

	}



}
